---
layout: "default"
title: "Hashable"
description: "Swift documentation for 'Hashable': A type that can be hashed into a Hasher to produce an integer hash value."
keywords: "Hashable,protocol,swift,documentation,==,hash,hashValue"
root: "/v4.2"
---

<div class="intro-declaration"><code class="language-swift">protocol Hashable</code></div>

<div class="discussion comment">
    <p>A type that can be hashed into a <code>Hasher</code> to produce an integer hash value.</p>

<p>You can use any type that conforms to the <code>Hashable</code> protocol in a set or as
a dictionary key. Many types in the standard library conform to <code>Hashable</code>:
Strings, integers, floating-point and Boolean values, and even sets are
hashable by default. Some other types, such as optionals, arrays and ranges
automatically become hashable when their type arguments implement the same.</p>

<p>Your own custom types can be hashable as well. When you define an
enumeration without associated values, it gains <code>Hashable</code> conformance
automatically, and you can add <code>Hashable</code> conformance to your other custom
types by implementing the <code>hash(into:)</code> method. For structs whose stored
properties are all <code>Hashable</code>, and for enum types that have all-<code>Hashable</code>
associated values, the compiler is able to provide an implementation of
<code>hash(into:)</code> automatically.</p>

<p>Hashing a value means feeding its essential components into a hash function,
represented by the <code>Hasher</code> type. Essential components are those that
contribute to the type&#39;s implementation of <code>Equatable</code>. Two instances that
are equal must feed the same values to <code>Hasher</code> in <code>hash(into:)</code>, in the
same order.</p>

<h1>Conforming to the Hashable Protocol</h1>

<p>To use your own custom type in a set or as the key type of a dictionary,
add <code>Hashable</code> conformance to your type. The <code>Hashable</code> protocol inherits
from the <code>Equatable</code> protocol, so you must also satisfy that protocol&#39;s
requirements.</p>

<p>The compiler automatically synthesizes your custom type&#39;s <code>Hashable</code> and
requirements when you declare <code>Hashable</code> conformance in the type&#39;s original
declaration and your type meets these criteria:</p>

<ul><li>For a <code>struct</code>, all its stored properties must conform to <code>Hashable</code>.</li><li>For an <code>enum</code>, all its associated values must conform to <code>Hashable</code>. (An
<code>enum</code> without associated values has <code>Hashable</code> conformance even without
the declaration.)</li></ul>

<p>To customize your type&#39;s <code>Hashable</code> conformance, to adopt <code>Hashable</code> in a
type that doesn&#39;t meet the criteria listed above, or to extend an existing
type to conform to <code>Hashable</code>, implement the <code>hash(into:)</code> method in your
custom type.</p>

<p>In your <code>hash(into:)</code> implementation, call <code>combine(_:)</code> on the provided
<code>Hasher</code> instance with the essential components of your type. To ensure
that your type meets the semantic requirements of the <code>Hashable</code> and
<code>Equatable</code> protocols, it&#39;s a good idea to also customize your type&#39;s
<code>Equatable</code> conformance to match.</p>

<p>As an example, consider a <code>GridPoint</code> type that describes a location in a
grid of buttons. Here&#39;s the initial declaration of the <code>GridPoint</code> type:</p>

<pre><code class="language-swift">/// A point in an x-y coordinate system.
struct GridPoint {
    var x: Int
    var y: Int
}</code></pre>

<p>You&#39;d like to create a set of the grid points where a user has already
tapped. Because the <code>GridPoint</code> type is not hashable yet, it can&#39;t be used
in a set. To add <code>Hashable</code> conformance, provide an <code>==</code> operator function
and implement the <code>hash(into:)</code> method.</p>

<pre><code class="language-swift">extension GridPoint: Hashable {
    static func == (lhs: GridPoint, rhs: GridPoint) -&gt; Bool {
        return lhs.x == rhs.x &amp;&amp; lhs.y == rhs.y
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(x)
        hasher.combine(y)
    }
}</code></pre>

<p>The <code>hash(into:)</code> method in this example feeds the grid point&#39;s <code>x</code> and <code>y</code>
properties into the provided hasher. These properties are the same ones
used to test for equality in the <code>==</code> operator function.</p>

<p>Now that <code>GridPoint</code> conforms to the <code>Hashable</code> protocol, you can create a
set of previously tapped grid points.</p>

<pre><code class="language-swift">var tappedPoints: Set = [GridPoint(x: 2, y: 3), GridPoint(x: 4, y: 1)]
let nextTap = GridPoint(x: 0, y: 1)
if tappedPoints.contains(nextTap) {
    print(&quot;Already tapped at (\(nextTap.x), \(nextTap.y)).&quot;)
} else {
    tappedPoints.insert(nextTap)
    print(&quot;New tap detected at (\(nextTap.x), \(nextTap.y)).&quot;)
}
// Prints &quot;New tap detected at (0, 1).&quot;)</code></pre>
</div>

<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<code class="inherits">Equatable</code>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>



<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>




<h3>Instance Variables</h3>
<div class="declaration" id="var-hashvalue_-int">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-hashvalue_-int">var hashValue: Int</a> <span class="required">Required</span><div class="comment collapse" id="comment-var-hashvalue_-int"><div class="p">
    <p>The hash value.</p>

<p>Hash values are not guaranteed to be equal across different executions of
your program. Do not save hash values to use during a future execution.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var hashValue: Int { get }</code>

    </div></div>
</div>



<h3>Instance Methods</h3>
<div class="declaration inherited" id="func-eqeq_rhs_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-eqeq_rhs_">func ==(<wbr>_:<wbr>rhs:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-eqeq_rhs_"><div class="p">
    <p>Returns a Boolean value indicating whether two values are equal.</p>

<p>Equality is the inverse of inequality. For any values <code>a</code> and <code>b</code>,
<code>a == b</code> implies that <code>a != b</code> is <code>false</code>.</p>

<p><strong>Parameters:</strong>
  <strong>lhs:</strong> A value to compare.
  <strong>rhs:</strong> Another value to compare.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func ==(lhs: Self, rhs: Self) -&gt; Bool</code>
    
        <h4>Declared In</h4>
        <a href="../../protocol/Equatable/"><code>Equatable</code></a>    
</div></div>
</div>
<div class="declaration" id="func-hash-into_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-hash-into_">func hash(<wbr>into:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-hash-into_"><div class="p">
    <p>Hashes the essential components of this value by feeding them into the
given hasher.</p>

<p>Implement this method to conform to the <code>Hashable</code> protocol. The
components used for hashing must be the same as the components compared
in your type&#39;s <code>==</code> operator implementation. Call <code>hasher.combine(_:)</code>
with each of these components.</p>

<p><strong>Important:</strong> Never call <code>finalize()</code> on <code>hasher</code>. Doing so may become a
  compile-time error in the future.</p>

<p><strong><code>hasher</code>:</strong>  The hasher to use when combining the components
  of this instance.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func hash(into hasher: inout Hasher)</code>
    
    
</div></div>
</div>


<h3>Default Implementations</h3>







<div class="declaration inherited" id="func--excleq_rhs_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func--excleq_rhs_">func !=(<wbr>_:<wbr>rhs:)</a>
        
<div class="comment collapse" id="comment-func--excleq_rhs_"><div class="p">
    <p>Returns a Boolean value indicating whether two values are not equal.</p>

<p>Inequality is the inverse of equality. For any values <code>a</code> and <code>b</code>, <code>a != b</code>
implies that <code>a == b</code> is <code>false</code>.</p>

<p>This is the default implementation of the not-equal-to operator (<code>!=</code>)
for any type that conforms to <code>Equatable</code>.</p>

<p><strong>Parameters:</strong>
  <strong>lhs:</strong> A value to compare.
  <strong>rhs:</strong> Another value to compare.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func !=(lhs: Self, rhs: Self) -&gt; Bool</code>
    
        <h4>Declared In</h4>
        <a href="../../protocol/Equatable/"><code>Equatable</code></a>    
</div></div>
</div>





